package com.cv.framework.common.pojo;

import com.cv.framework.common.exception.ErrorCode;
import com.cv.framework.common.exception.constants.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * 响应
 *
 * @param <T>
 * @author Charles_XDXD
 */
@Data
public class Result<T> implements Serializable {

    /**
     * 响应码
     *
     * @see ErrorCode#getCode()
     */
    private Integer code;

    /**
     * 响应信息
     *
     * @see ErrorCode#getMsg()
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public static <T> Result<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> Result<T> error(Integer code, String msg) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "服务端异常：响应的 Code 异常。");
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setMsg(GlobalErrorCodeConstants.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setMsg(GlobalErrorCodeConstants.SUCCESS.getMsg());
        return result;
    }

}
